﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Lion.WeiXin.Core.Data;
using RazorEngine.Templating;
using Lion.WeiXin.Tools.Log;
namespace Lion.WeiXin.Core
{
    /// <summary>
    /// 模板引擎生成器
    /// </summary>
    public class TemplateEngine
    {
        DBContext db = new DBContext();
        /// <summary>
        /// 站点域名: http://www.163.com
        /// </summary>
        string _WebSiteUrl = "";
        /// <summary>
        /// 文件物理路径根 d:/ee/rr
        /// </summary>
        string _Root = "";

        public TemplateEngine(string webSiteUrl, string fileRoot)
        {
            this._WebSiteUrl = webSiteUrl;
            this._Root = fileRoot;
        }

        /// <summary>
        /// 生成文章列表
        /// </summary>
        /// <param name="articles"></param>
        public void GenerateArticleListToHtml(List<Models.ArticleModel> articles)
        {
            foreach (Models.ArticleModel item in articles)
            {
                 GenerateArticleToHtml(item);
            }
        }

        /// <summary>
        /// 生成栏目列表
        /// </summary>
        /// <param name="categorys"></param>
        public void GenerateCategoryListToHtml(List<Models.CategoryModel> categorys)
        {
            foreach (Models.CategoryModel item in categorys)
            {               
                GenerateCategoryToHtml(item);
            }
        }

        /// <summary>
        /// 生成文章静态html
        /// </summary>
        /// <param name="model"></param>
        public void GenerateArticleToHtml(Models.ArticleModel article)
        {
            Models.TemplateModel tempModel = db.Templates.GetModel(article.TemplateID);
            if (tempModel == null)
            {
                LogHelper.Info("文章{0}的模板为空，生成结束!", article.AID);
                return;
            }
            //获取当前文章模板
            string template = ReadFileContent(_Root + tempModel.FilePath);

            TemplateEngineModel model = new TemplateEngineModel();
            model.Article = article;
            string html = RazorEngine.Engine.Razor.RunCompile(template, tempModel.Name, null, model);

            CreateHtml(_Root + article.Url, html);
        }
        /// <summary>
        /// 生成栏目静态html
        /// </summary>
        /// <param name="category"></param>
        /// <param name="tempModel"></param>
        public void GenerateCategoryToHtml(Models.CategoryModel category)
        {
            //获取当前文章模板
            Models.TemplateModel tempModel = db.Templates.GetModel(category.TemplateID);
            if (tempModel == null)
            {
                LogHelper.Info("栏目[{0}]的模板为空，生成结束!", category.Name);
                return;
            }

            string template = ReadFileContent(_Root + tempModel.FilePath);

            TemplateEngineModel model = new TemplateEngineModel();
            model.Category = category;
            string html = RazorEngine.Engine.Razor.RunCompile(template, tempModel.Name, null, model);

            CreateHtml(_Root + category.Url, html);
        }

        private void CreateHtml(string fileName, string content)
        {
            //判断当前文件下文件夹是否存在
            string dir = Path.GetDirectoryName(fileName);
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    sw.Write(content);
                }
            }
        }

        private string ReadFileContent(string fileName)
        {
            string result = "";
            using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
            {
                using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                {
                    result = reader.ReadToEnd();
                }
            }
            return result;
        }
    }
}
